package lk.mrt.cse.wihidum.storage;

import lk.mrt.cse.wihidum.storage.*;
import java.util.*;
import org.apache.log4j.Logger;

/**
 * Created by IntelliJ IDEA.
 * User: Wihidum
 * Date: Mar 26, 2010
 * Time: 8:46:40 AM
 * To change this template use File | Settings | File Templates.
 */
public class SubscriptionMap 
{
       private static final Logger logger = Logger.getLogger (SubscriptionMap.class);
       private static Map<SubscriberIdentifier,Subscriber> subMap=new HashMap<SubscriberIdentifier,Subscriber>();
      // private static ArrayList<Subscriber> filteredSubscribers=new ArrayList<Subscriber>();
       public static boolean saveSubscriber(SubscriberIdentifier subIdentifier,Subscriber subscriber)
       {
        boolean savedSuccessfully=false;
        if(subMap.isEmpty())
        {
             subMap.put(subIdentifier,subscriber);
             logger.info("Subscriber: "+subIdentifier.getClientID()+" was added to list.");
             savedSuccessfully=true;
             return savedSuccessfully;
        }
        else
        {
          boolean alreadySubscribed=false;
          for(Map.Entry<SubscriberIdentifier,Subscriber> entry:subMap.entrySet())
          {
               Subscriber currentListSubscriber=entry.getValue();
               String currentListSubscriberID=currentListSubscriber.getSubscriberIdentifier().getClientID();
               String currentListSubscriberFilter=currentListSubscriber.getSubscriberIdentifier().getFilterTopic();
               if(((currentListSubscriberID.equals(subIdentifier.getClientID())))&&(currentListSubscriberFilter.equals(subIdentifier.getFilterTopic())))
               {
                   alreadySubscribed=true;
                   logger.info("Subscriber: "+subIdentifier.getClientID()+":"+subIdentifier.getFilterTopic()+" was already in the list.");
                   savedSuccessfully=false;
                   break;
                   /*subMap.put(subIdentifier,subscriber);
                   logger.info("Subscriber: "+subIdentifier.getClientID()+" was added to list.");
                   savedSuccessfully=true; */
               }
                           
            }
            if(!alreadySubscribed)
              {
                  subMap.put(subIdentifier,subscriber);
                  logger.info("Subscriber: "+subIdentifier.getClientID()+" was added to list.");
                  savedSuccessfully=true;
              }

            return savedSuccessfully;
        }

       }

       public static ArrayList<Subscriber> getSubscribersFilteredByTopic(String topic)
       {
            ArrayList<Subscriber> filteredSubscribersList =new ArrayList<Subscriber>();
            String[] topicSplitArray = topic.split("#");
            ArrayList<String> filters = new ArrayList<String>();
            String temp = topicSplitArray[0];

            for(int i = 0; i < topicSplitArray.length; i++)
            {
                if(i == 0)
                    filters.add(temp);
                else
                {
                    temp = temp.concat("#"+topicSplitArray[i]);
                    filters.add(temp);
                }
            }

           for(String filter: filters)
           {               
                for(Map.Entry<SubscriberIdentifier,Subscriber> entry:subMap.entrySet())
                {
                    if(filter.equals(entry.getKey().getFilterTopic()))
                    {
                        Subscriber matchingSubscriber=entry.getValue();
                        matchingSubscriber.setSubscriberIdentifier(entry.getKey());
                        filteredSubscribersList.add(matchingSubscriber);
                    }
                }
           }
           return filteredSubscribersList;
       }

       public static Map<SubscriberIdentifier,Subscriber> getAllSubscribers()
       {
              return subMap;
       }

       public static String deleteSubscriber(String subscriberUUID)
       {
          String topic=null;
          for(Map.Entry<SubscriberIdentifier,Subscriber> entry:subMap.entrySet())
          {
               if(subscriberUUID.equals(entry.getValue().getUuid()))
               {
                   topic=entry.getValue().getSubscriberIdentifier().getFilterTopic();
                   subMap.remove(entry.getKey());
                   break;
               }
          }
          return topic;
      }
     public static boolean isSubscribersExistonTopic(String topic)
     {
         boolean exist=false;
         if(!subMap.isEmpty())
         {
             for(Map.Entry<SubscriberIdentifier,Subscriber> entry:subMap.entrySet())
              {
                   String currentTopic=entry.getValue().getSubscriberIdentifier().getFilterTopic();
                   if(currentTopic!=null && topic.equals(currentTopic))
                   {
                       exist=true;
                       break;
                   }
              }
         }

         return exist;
     }
}
